import { d_sort, cline, is_empty, loc } from 'sui';
//import { err_col } from 'nui';
import { spc_obj } from '../cmd';

const tp_name = (v) => {
    const tmp = {
        xbar: "XBar" + loc("db_097"),
        np: "NP" + loc("db_097"),
        c: "C" + loc("db_097"),
        p: "P" + loc("db_097"),
        u: "U" + loc("db_097"),
    };

    return tmp[v];
};

const reload = (para, setMain, fnDone=null) => {
    /*
    urlGet(fnDone,
        {
            n: "spc",
            obj: spc_obj,
        },
        r => {
            d_sort(r, "sn");
            let db = null;
            let sta = null;

            if (para) {
                db = para["cell"];
                sta = para["station"];
            }

            const db_val = {};
            const wsn_val = {};

            if (!is_empty(sta)) {
                sta.forEach(d => wsn_val[d.sn] = d.name);
            }

            if (!is_empty(db)) {
                db.forEach(d => {
                    const kemy = d["keym"];
                    const kemd = d["keyd"];
                    if (is_empty(keym)) {
                        return;
                    }

                    const tmp = {};

                    keym.forEach((t, i) => tmp[t] = keyd[i]);

                    db_val[d.sn] = {
                        name: d.name,
                        k: tmp,
                    };
                });
            }

            r.forEach((d, i) => {
                d["index"] = i + 1;
                d["ltp"] = tp_name(d["tp"]);

                let tmp = db_val[d["dsn"]];
                if (tmp) {
                    d["ldsn"] = tmp.name;
                }

                tmp = tmp.k[d["fsn"]];
                if (tmp) {
                    d["lfsn"] = tmp;
                }

                tmp = wsn_val[d["wsn"]];
                if (tmp) {
                    d["lwsn"] = tmp;
                }
            });

            setMain(r);
        }, e => setMain(null));
        */
};

const importCfg = (fnDone, data, gcol, main, para, umd, setMain) => {
    fnDone();

    umd.startWait();

    const col = gcol.filter(d => d.key !== "index" && d.key !== "e_opt");

    if (is_empty(col)) {
        umd.setWrong(loc("z_55"));
        return;
    }

    if (is_empty(data)) {
        umd.setWrong(loc("mt_13"));
        return;
    }

    const head = col.map(d => d.name);
    /*
    if (err_col(data[0], head)) {
        umd.setWrong(loc("db_018"));
        return;
    }
    */

    const add = [];

    for (let i = 1; i < data.length; i++) {
        const d = data[i];
        const tmp = [];

        col.forEach((t, i) => {
            let key = t.key;

            if (key === "ltp") {
                key = "tp";
            }
            else if (key === "ldsn") {
                key = "dsn";
            }
            else if (key === "lfsn") {
                key = "fsn";
            }
            else if (key === "lwsn") {
                key = "wsn";
            }

            tmp.push({k: key, v: "" + d[i]});
        });

        add.push(tmp);
    }

    if (is_empty(add)) {
        umd.setWrong(loc("mt_13"));
        return;
    }

    const cmd = {a: [{
        u: cline(),
        n: "spc",
        v: add,
    }]};

    if (!is_empty(main)) {
        const tmp = [];
        main.forEach(d => tmp.push(d.uid));

        cmd["r"] = [{
            u: cline(),
            v: [{k: "spc", u: tmp.join(",")}],
        }];
    }

    //urlM(null, cmd, r => reload(para, setMain, umd.endWait), e => umd.setWrong(loc("mg_127")));
};

const fix_col = () => {
    return [
        {name: "SN", key: 'sn', width: 100},
        {name: loc("cmd_097"), key: 'name', width: 100},
        {name: loc("mg_007"), key: 'ltp', width: 90},
        {name: loc("mg_163"), key: 'ldsn', width: 140},
        {name: `${loc("mg_163")}${loc("db_020")}`, key: 'lfsn', width: 140},
        {name: loc("equip_12"), key: 'lwsn', width: 120},
        {name: loc("product_38"), key: 'num', width: 80},
        {name: "UCL", key: 'ucl', width: 70},
        {name: "CL", key: 'cl', width: 70},
        {name: "LCL", key: 'lcl', width: 70},
        {name: "USL", key: 'usl', width: 70},
        {name: "SL", key: 'sl', width: 70},
        {name: "LSL", key: 'lsl', width: 70},

    ];
};

const c_load = {
    nd: [{
        n: "cell",
        obj: "sn,name,keym,keyd",
    }, {
        n: "station",
        obj: "sn,name",
    }]
};

export {c_load, fix_col, tp_name, importCfg, reload};
